﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using MSUtil;
using MonitoringIISWebSite.Common;

namespace MonitoringIISWebSite.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
/***********************CECI EST UN EXEMPLE D'UTILISATION DE LOG PARSER**********************************/
        /// <summary>
        /// Récupère le temps total des requêtes exécuté
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public double ParseW3CLog(string userID)
        {

            // prepare LogParser Recordset & Record objects
            ILogRecordset rsLP = null;
            ILogRecord rowLP = null;

            LogQueryClassClass LogParser = null;
            COMW3CInputContextClassClass W3Clog = null;

            double UsedBW = 0;
            int Unitsprocessed;
            double sizeInBytes;

            string strSQL = null;

            LogParser = new LogQueryClassClass();
            W3Clog = new COMW3CInputContextClassClass();

            try
            {
                //W3C Logparsing SQL. Replace this SQL query with whatever 
                //you want to retrieve. The example below 
                //will sum up all the bandwidth
                //Usage of a specific folder with name 
                //"userID". Download Log Parser 2.2 
                //from Microsoft and see sample queries.

                strSQL = @"SELECT SUM(time-taken) from C:\\inetpub\\logs\\LogFiles\\W3SVC1" +
                         @"\\*.log WHERE cs-uri-stem LIKE '%/" +
                         userID + "/%' ";

                // run the query against W3C log
                rsLP = LogParser.Execute(strSQL, W3Clog);

                rowLP = rsLP.getRecord();

                Unitsprocessed = rsLP.inputUnitsProcessed;

                if (rowLP.getValue(0).ToString() == "0" ||
                    rowLP.getValue(0).ToString() == "")
                {
                    //Return 0 if an err occured
                    UsedBW = 0;
                    return UsedBW;
                }

                //Bytes to MB Conversion
                double Bytes = Convert.ToDouble(rowLP.getValue(0).ToString());
                UsedBW = Bytes / (1024 * 1024);

                //Round to 3 decimal places
                UsedBW = Math.Round(UsedBW, 3);
            }
            catch
            {
                throw;
            }

            return UsedBW;
        }


        //Attention il faut changer le strSQL pour pouvoir logger d'autres appli en fonction de où se trouvent les applis
        /// <summary>
        /// Récupère les date et les User Agents pour une application IIS
        /// </summary>
        /// <returns></returns>
        public DataTableSource ParseW3CLog()
        {

            // prepare LogParser Recordset & Record objects
            ILogRecordset rsLP = null;
            ILogRecord rowLP = null;
            DataTableSource dt = new DataTableSource();
            dt.Columns.Add("date_time");
            dt.Columns.Add("user_agent");

            LogQueryClassClass LogParser = null;
            COMW3CInputContextClassClass W3Clog = null;


            string strSQL = null;

            LogParser = new LogQueryClassClass();
            W3Clog = new COMW3CInputContextClassClass();

            try
            {
                //W3C Logparsing SQL. Replace this SQL query with whatever 
                //you want to retrieve. The example below 
                //will sum up all the bandwidth
                //Usage of a specific folder with name 
                //"userID". Download Log Parser 2.2 
                //from Microsoft and see sample queries.

                strSQL = @"SELECT * from C:\\inetpub\\logs\\LogFiles\\W3SVC1" +
                         @"\\*.log ";

                // run the query against W3C log
                rsLP = LogParser.Execute(strSQL, W3Clog);

                do
                {
                    DataRow dr = dt.NewRow();
                    rowLP = rsLP.getRecord();
                    String d = rowLP.getValue("date").ToString();
                    d = DateTime.Parse(d).ToShortDateString();
                    String t = rowLP.getValue("time").ToString();
                    t = DateTime.Parse(t).ToShortTimeString();
                    dr["date_time"] = d + " " + t;
                    dr["user_agent"] = rowLP.getValue("cs(User-Agent)");

                    dt.Rows.Add(dr);
                    rsLP.moveNext();
                }
                while (!rsLP.atEnd());
                

               
            }
            catch
            {
                throw;
            }
            return dt;
        }
    }

}
